package com.ruoyi.socket.util.anbaobr;

/***
 *  @description
 *  @author zhao
 *  @date: 2020/6/12
 *  @version: v1.0
 */


import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class Utils {

    //监听端口
    private static final int PORT = 8234;

    public static void main(String[] args) throws Exception {

        ServerSocket serverSocket = null;
        Socket socket = null;
        try {
            //建立服务器的Socket，并设定一个监听的端口PORT
            serverSocket = new ServerSocket(PORT);
            //由于需要进行循环监听，因此获取消息的操作应放在一个while大循环中
            while (true) {
                try {
                    //建立跟客户端的连接
                    socket = serverSocket.accept();
                } catch (Exception e) {
                    System.out.println("建立连接出现异常");
                    e.printStackTrace();
                }
//                 String str = "<32Mo00000000ffffe40705140c321c04800800115136323839363206>";
//                 System.out.println("监测："+MyAnbaoSdk.hexString2binaryString(str.substring(9,13)).substring(0,1));
//                 开启接收数据的线程
                ReceiveThread thread1 = new ReceiveThread(socket);
                thread1.start();

                //开启发送数据的线程
                SendThread thread2 = new SendThread(socket);
                thread2.start();
            }
        } catch (Exception e) {
            System.out.println("端口被占用");
            e.printStackTrace();
        } finally {
            serverSocket.close();
        }

    }
}

class ReceiveThread extends Thread {
    InputStream inputStream;
    OutputStream outputStream;
    private Socket socket;

    public ReceiveThread(Socket socket) {
        this.socket = socket;
    }

    public void run() {
        try {
            while (true) {
                //接收客户端的消息并打印
//              System.out.println(socket);
                inputStream = socket.getInputStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
                String data = br.readLine();
                System.out.println(data);
                if (SdkParse.parseText(data) == null) {
                    System.out.println("解析出错：" + data);
                } else {
                    Map<String, String> tmpmap = new HashMap<String, String>();
                    tmpmap = SdkParse.parseText(data);
                    for (Entry<String, String> entry : tmpmap.entrySet()) {
                        String mapKey = entry.getKey();
                        String mapValue = entry.getValue();
                        System.out.println(mapKey + ":" + mapValue);
                    }
                }


            }
        } catch (Exception e) {
            System.out.println("主动断开连接了");
            //e.printStackTrace();
        }
        //操作结束，关闭socket
        try {
            socket.close();
        } catch (IOException e) {
            System.out.println("连接出现异常");
            e.printStackTrace();
        }
    }
}


class SendThread extends Thread {
    InputStream inputStream;
    OutputStream outputStream;
    private Socket socket;

    public SendThread(Socket socket) {
        this.socket = socket;
    }

    public void run() {
        try {
            //第一次需要向呼吸机发送消息Ah/Ai/Aj/Ak来请求
            outputStream = socket.getOutputStream();
            outputStream.write(SdkParse.generateAh().getBytes());
            outputStream.write(SdkParse.generateAi().getBytes());
            outputStream.write(SdkParse.generateAj().getBytes());
            outputStream.write(SdkParse.generateAk().getBytes());
            System.out.println("send Ah/Ai/Aj/Ak succeed!");
            int cnt = 0;
            while (true) {

                //每一秒向呼吸机发送一次An消息
                outputStream = socket.getOutputStream();
                outputStream.write(SdkParse.generateAn().getBytes());
                System.out.println("send An succeed!");
                sleep(1000);
                cnt++;
                //发送设置通气报警阈值Am消息
                if (cnt == 6 || cnt == 24) {
                    String meString = SdkParse.generateAm("ffff", "3c", "01", "06", "01", "3c", "14", "ff", "ff", "c800", "1400", "3c", "14");
                    outputStream.write(meString.getBytes());
                    System.out.println("send Am succeed!:" + meString);
                }
                //发送设置通气控制参数Al消息
                if (cnt == 12 || cnt == 30) {
                    String tmp = SdkParse.generateAl("ffff", "00", "05", "64", "01f4", "000a", "0013", "0001", "1e", "00", "0900", "01ff", "00", "ff", "00", "0c", "0f", "28", "14", "0c", "ff", "ff", "0000");
                    outputStream.write(tmp.getBytes());
                    System.out.println("send Al succeed!:" + tmp);
                }
                //发送设置系统参数Ap消息
                if (cnt == 5 || cnt == 36) {
                    String tmp = SdkParse.generateAp("00", "01", "01", "01", "00", "00", "01", "01", "00", "00");
                    outputStream.write(tmp.getBytes());
                    System.out.println("send Ap succeed!:" + tmp);
                }

                //APP服务器关闭包(Ao)
                outputStream.write(SdkParse.generateAo().getBytes());
            }
        } catch (Exception e) {
            System.out.println("主动断开连接了");
            //e.printStackTrace();
        }
        //操作结束，关闭socket
        try {
            socket.close();
        } catch (IOException e) {
            System.out.println("关闭连接出现异常");
            e.printStackTrace();
        }
    }
}


